package com.greenskinmonster.a51nb.job;

import android.content.Context;
import android.text.TextUtils;

import com.greenskinmonster.a51nb.async.LoginHelper;
import com.greenskinmonster.a51nb.bean.DetailListBean;
import com.greenskinmonster.a51nb.bean.HiSettingsHelper;
import com.greenskinmonster.a51nb.okhttp.NetworkError;
import com.greenskinmonster.a51nb.okhttp.OkHttpHelper;
import com.greenskinmonster.a51nb.parser.HiParser;
import com.greenskinmonster.a51nb.parser.ThreadDetailParser;
import com.greenskinmonster.a51nb.ui.ThreadDetailFragment;
import com.greenskinmonster.a51nb.utils.Constants;
import com.greenskinmonster.a51nb.utils.HiUtils;
import com.greenskinmonster.a51nb.utils.Logger;
import com.greenskinmonster.a51nb.utils.Utils;

import org.greenrobot.eventbus.EventBus;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

/**
 * Created by GreenSkinMonster on 2016-11-11.
 */

public class ThreadDetailJob extends BaseJob {

    public final static String FIND_AUTHOR_ID = "-1";
    private final static int MIN_JOB_TIME_MS = 150;

    private Context mCtx;
    private String mTid;
    private String mAuthorId;
    private String mGotoPostId;
    private int mPage;
    private int mFetchType;

    private ThreadDetailEvent mEvent;

    public ThreadDetailJob(Context context, String sessionId, String tid, String authorId, String gotoPostId, int page, int fetchType, int loadingPosition) {
        super(sessionId);
        mCtx = context;
        mTid = tid;
        mAuthorId = authorId;
        mGotoPostId = gotoPostId;
        mPage = page;
        mFetchType = fetchType;

        mEvent = new ThreadDetailEvent();
        mEvent.mSessionId = mSessionId;
        mEvent.mFectchType = fetchType;
        mEvent.mPage = page;
        mEvent.mLoadingPosition = loadingPosition;
    }

    @Override
    public void onAdded() {
        mEvent.mStatus = Constants.STATUS_IN_PROGRESS;
        EventBus.getDefault().postSticky(mEvent);
    }

    @Override
    public void onRun() throws Throwable {
        long start = System.currentTimeMillis();
        DetailListBean data = null;
        int eventStatus = Constants.STATUS_SUCCESS;
        String eventMessage = "";
        String eventDetail = "";

        for (int i = 0; i < OkHttpHelper.MAX_RETRY_TIMES; i++) {
            try {
                String resp = fetchDetail();
                if (resp != null) {
                    Document doc = Jsoup.parse(resp);

                    if (!LoginHelper.checkLoggedin(doc)) {
                        if (HiSettingsHelper.getInstance().isLoginInfoValid()) {
                            int status = new LoginHelper().login();
                            if (status == Constants.STATUS_SUCCESS) {
                                continue;
                            }
                        }
                    }

                    String tid = HiUtils.isValidId(mTid) ? mTid : Utils.getMiddleString(resp, "tid = parseInt('", "')");
                    data = ThreadDetailParser.parse(doc, tid);
                    if (data == null || data.getCount() == 0) {
                        eventStatus = Constants.STATUS_FAIL_ABORT;
                        eventMessage = "页面加载失败";
                        String message = HiParser.parseErrorMessage(doc);
                        if (!TextUtils.isEmpty(message)) {
                            eventMessage = message;
                        }
                    }
                    break;
                }
            } catch (Exception e) {
                Logger.e(e);
                NetworkError networkError = OkHttpHelper.getErrorMessage(e);
                eventStatus = Constants.STATUS_FAIL;
                eventMessage = networkError.getMessage();
                eventDetail = networkError.getDetail();
                if (isCancelled())
                    break;
            }
        }

        long delta = System.currentTimeMillis() - start;
        if (delta < MIN_JOB_TIME_MS) {
            Thread.sleep(MIN_JOB_TIME_MS - delta);
        }

        mEvent.mData = data;
        mEvent.mStatus = eventStatus;
        mEvent.mMessage = eventMessage;
        mEvent.mDetail = eventDetail;
        mEvent.mAuthorId = mAuthorId;
        EventBus.getDefault().postSticky(mEvent);
    }

    private String fetchDetail() throws Exception {
        String mUrl;
        if (!TextUtils.isEmpty(mGotoPostId)) {
            if (TextUtils.isEmpty(mTid))
                mUrl = HiUtils.GotoPostUrl.replace("{pid}", mGotoPostId);
            else
                mUrl = HiUtils.RedirectToPostUrl.replace("{tid}", mTid).replace("{pid}", mGotoPostId);
        } else if (mPage == ThreadDetailFragment.LAST_PAGE) {
            mUrl = HiUtils.LastPageUrl.replace("{tid}", mTid);
        } else {
            mUrl = HiUtils.DetailListUrl + mTid + "&page=" + mPage;
            if (FIND_AUTHOR_ID.equals(mAuthorId)) {
                mAuthorId = getThreadAuthorId();
            }
            if (HiUtils.isValidId(mAuthorId))
                mUrl += "&authorid=" + mAuthorId;
        }
        return OkHttpHelper.getInstance().get(mUrl, mSessionId, OkHttpHelper.FORCE_NETWORK);
    }

    private String getThreadAuthorId() {
        try {
            String url = HiUtils.DetailListUrl + mTid + "&page=1";
            String response = OkHttpHelper.getInstance().get(url, mSessionId, OkHttpHelper.PREFER_CACHE);
            Document doc = Jsoup.parse(response);
            return ThreadDetailParser.getThreadAuthorId(doc);
        } catch (Exception e) {
            return "";
        }
    }

}
